package com.lkd.handler;

import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.SupplyContract;
import com.lkd.contract.TaskCompleteContract;
import com.lkd.emq.Topic;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.io.IOException;

@Component
@Topic(TopicConfig.VMS_SUPPLY_TOPIC)
@Slf4j
public class SupplyCompletedHandler implements MsgHandler {

    @Autowired
    private VendingMachineService vendingMachineService;

    @Override
    public void process(String jsonMsg) throws IOException {

        if(StringUtils.isEmpty(jsonMsg)){
            log.error(TopicConfig.VMS_SUPPLY_TOPIC + "消息内容为空");
            return;
        }

        SupplyContract contract = JsonUtil.getByJson(jsonMsg, SupplyContract.class);
        //校验消息内容
        if(contract == null || StringUtils.isEmpty(contract.getInnerCode())
            || CollectionUtils.isEmpty(contract.getSupplyData())
        ){
            log.error(TopicConfig.VMS_SUPPLY_TOPIC + "消息内容不正确:" + jsonMsg);
            return;
        }

        vendingMachineService.supply(contract);

    }
}
